//
//  JOSettingCell.m
//  网易彩票
//
//  Created by song on 15/3/5.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOSettingCell.h"
#import "JOSettingItem.h"
#import "JOSettingArrowItem.h"
#import "JOSettingSwitchItem.h"


@interface JOSettingCell()

@property(nonatomic , weak) UIView *arrowView;

@property(nonatomic , weak) UISwitch *switchView;

@end

@implementation JOSettingCell


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        
    }
    return self;
}

-(UIView *)arrowView
{
    if(_arrowView ==nil)
    {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
    }
    return _arrowView;
}

-(UISwitch *)switchView
{
    if(_switchView==nil)
    {
        _switchView =[[UISwitch alloc] init];
        [_switchView addTarget:self action:@selector(switchStateChange) forControlEvents:UIControlEventValueChanged];
        
    }
    return _switchView;
}

-(void)switchStateChange
{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setBool:self.switchView.isOn forKey:self.item.title];
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID=@"setting";
    JOSettingCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil)
    {
        cell = [[JOSettingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
    }
    return cell;
}

- (void)awakeFromNib {

}

-(void)setItem:(JOSettingItem *)item
{
    _item = item;
    self.imageView.image = [UIImage imageNamed:item.icon];
    self.textLabel.text =item.title;
    
    if([item isKindOfClass:[JOSettingArrowItem class]])
    {
        self.accessoryView = self.arrowView;
    }else if([item isKindOfClass:[JOSettingSwitchItem class]])
    {
        self.accessoryView = self.switchView;
    }else{
        self.accessoryView = nil;
    }

}

@end
